<?php
/**
* PHP Template.
*/
class LocationsModel extends Model {
	public function __construct() {
			parent::Model();
		}
	public function create($lDataArr,$c_id) {
			$query = "INSERT INTO locations(name,address1,address2,city,state,zipcode,location_type,xLat,yLat,contact_id) VALUES(?,?,?,?,?,?,?,?,?,?)";
			$queryList = array($lDataArr["name"],
				$lDataArr["address1"],
				$lDataArr["address2"],
				$lDataArr["city"],
				$lDataArr["state"],
				$lDataArr["zipcode"],
				$lDataArr["location_type"],
				$lDataArr["xLat"],
				$lDataArr["yLat"],
				$c_id
				);
			$this->db->query($query,$queryList);
			if ($this->db->trans_status() === FALSE) {
					echo "<h2>ERROR Creating Locations</h2><br/><br/>" . $this->db->last_query();
				} else {
					$rs = $this->db->query("SELECT LAST_INSERT_ID() as location_id FROM locations");
					if($rs->num_rows()>0) {
							$l_id = $rs->row()->location_id;
						}
				}
			return $l_id;
		}
	public function update($lDataArr,$c_id,$l_id) {
			$query = "UPDATE locations SET name=?,address1=?,address2=?,city=?,state=?,zipcode=?,location_type=?,xLat=?,yLat=?,contact_id=? WHERE location_id=?";
			$queryList = array($lDataArr["name"],
				$lDataArr["address1"],
				$lDataArr["address2"],
				$lDataArr["city"],
				$lDataArr["state"],
				$lDataArr["zipcode"],
				$lDataArr["location_type"],
				$lDataArr["xLat"],
				$lDataArr["yLat"],
				$c_id,
				$l_id
				);
			$this->db->query($query,$queryList);
			if ($this->db->trans_status() === FALSE) {
					echo "<h2>ERROR SUBMITTING</h2><br/><br/>" . $this->db->last_query();
				}
		}
	public function getLocation($locName){
			$rs = $this->db->query("SELECT location_id FROM locations WHERE name LIKE ?",array($locName));
			if($rs->num_rows()>0){
				 return $this->row()->location_id;
			 }
			 return null;
		}
	public function getLocationByUserId($userId) {
			$query = "SELECT 
				name,
				address1,
				address2,
				city,
				state,
				zipcode,
				location_type,
				xLat,
				yLat
				FROM locations
				WHERE location_id=?";
			$queryData = array($userId);
			$rs = $this->db->query($query,$queryData);
			return $rs->row_array();
		}
}
?>